<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function()
{
	return View::make('hello');
});
Route::get('home',function(){
	return View::make('home');
});

//-------------------------------------function register------------------------------

Route::get('register',function(){
	return View::make('register');
});
Route::post('register',function(){
	$rules = array(
		"username"=>"required|min:3",
		"password"=>"required|min:6",
		"email"=>"required|email");
	if(!Validator::make(Input::all(),$rules)->fails() && User::check_username(Input::get('username')) && User::check_email(Input::get('email'))){
		User::save_user(Input::get('username'),Hash::make(Input::get('password')),Input::get('email'),Input::get('sex'));
		$data['register_success'] = "Congratulations! ".Input::get('username')." has successfully registered ";
		return View::make('login',$data);
	}
});
Route::post('check-username',function(){
	if(User::check_username(Input::get('username')))
		return "true";
	else
		return "false";
});
Route::post('check-email',function(){
	if(User::check_email(Input::get('email')))
		return "true";
	else
		return "false";
});
//-------------------------------------function login------------------------------

Route::get('login',function(){
	return View::make('login');
});
Route::post('login','HomeController@login');

//-------------------------------------function logout------------------------------

Route::get('logout',function(){
	Session::forget("logined");
	Session::forget("role");
	Session::forget("username");
	return Redirect::to('login');
});

//-------------------------------------function admin------------------------------

Route::group(array("before"=>"check-login-admin"),function(){
	Route::get('admin/home','AdminController@home');
	Route::get('admin/create','AdminController@create');
	Route::get('admin/insert','AdminController@insert');
});

//-------------------------------------function user------------------------------

Route::group(array("before"=>"check-login-user"),function(){
	Route::get('user/home','UserController@home');
	Route::get('user/setting','UserController@setting');
	Route::get('user/about','UserController@about');
});

